<?php
namespace Admin\Controller;

use Utuo\BackgroundController;

class ThemeController extends BackgroundController{
	
	protected function _initialize(){
		$this->setControllerTitle("主题管理 ");
		$this->setActionTitle(array(
				'index' => '主题列表',
				'add' => '主题添加',
				'modify' => '主题修改',
				'recycle' => '回收站'
		));
		$this->setActionNav(array(
				'index',
				'add',
				'recycle'
		));
	}
	
	// 客户主题列表
	public function indexAction() {
		$this->getThemeList(0);
	}

	//回收站
	public function recycleAction() {
		$this->getThemeList(1);
	}
	
	// 获得当前合作商的主题
	private function getThemeList($is_deleted) {
		$pageSize = $this->pageSize;
		$condition = array(
			"is_deleted" => $is_deleted,
		);
		$m_sql = M()->table("__THEME__ this0")->join("LEFT JOIN __PARTNER__ this1 on this0.partner=this1.id")
										->join("LEFT JOIN __CUSTOMER__ this2 on this0.customer=this2.id")
										->join("LEFT JOIN __MODEL__ this3 on this0.model_id=this3.id")
										->field("this0.*, this2.name as customerName, this1.name as partnerName, this3.name as modelName")
										->select(false);
		$themeLists = M()->table($m_sql." this0")->where($condition)->limit($pageSize)->page($this->getPage())->order('id DESC')->select();
		$count =  M()->table($m_sql." this0")->where($condition)->count();
		$this->assign("themeLists", $themeLists);
		$this->showPage($count, $pageSize);
		$this->display();
	}
	
	// 添加主题页
	public function addAction() {
		$customerLists = M("Customer")->getField("id,name");
		$partnerLists = M("Partner")->getField("id,name");
		$modelLists = M("Model")->getField("id,name");
		$this->assign("modelOptions",$this->getOption($modelLists, '', "==请选择适用模型=="));
		$this->assign("customerOptions",$this->getOption($customerLists, "", "==请选择客户=="));
		$this->assign("partnerOptions",$this->getOption($partnerLists, "", "==请选择合作商=="));
		$this->display();
	}
	
	// 添加主题
	public function addPostAction() {
		$d = D("Theme");
		if ($d->create()) {
			if ($d->add()) {
				$this->ajaxSuccess();
			} else {
				$this->ajaxError();
			}
		} else {
			$this->ajaxError($d->getError());
		}
	}
	
	// 修改主题
	public function modifyAction() {
		$d = D("Theme");
		$theme = $this->getOneById("Theme");
		$partnerLists = M("Partner")->getField("id,name");
		$customerLists = M("Customer")->where("partner={$theme['partner']}")->getField("id,name");
		$modelLists = M("Model")->getField("id,name");
		$theme["config"] =  $d->parseArray(unserialize($theme['config']));
		$theme["modelOptions"] = $this->getOption($modelLists, $theme['model_id'], "==请选择适用模型==");
		$theme["partnerOptions"] = $this->getOption($partnerLists, $theme['partner'], "==请选择合作商==");
		$theme["customerOptions"] = $this->getOption($customerLists, $theme['customer'], "==请选择客户==");
		$this->assign("theme",$theme);
		$this->display();
	}
	
	// 主题列表
	public function getCustomerPostAction() {
		$id = I("id",0,"intval");
		$partner = I("partner",0,"intval");
		$customerLists = M("Customer")->where(array("partner"=>$partner))->getField("id,name");
		$customer = M("Theme")->where("id=$id")->getField('customer');
		echo $this->getOption($customerLists, $customer, "==请选择客户==");
	}
	
	// 修改主题
	public function modifyPostAction() {
		$d = D("Theme");
		if ($d->create()) {
			if (false !== $d->save()) {
				$this->ajaxSuccess();
			} else {
				$this->ajaxError($d->getError());
			}
		} else {
			$this->ajaxError($d->getError());
		}
	}
	
	
	// 移除主题
	public function removePostAction() {
		$this->ajaxRemove();
	}
	
	// 恢复主题
	public function restorePostAction() {
		$this->ajaxRestore();
	}
	
	// 删除主题
	public function deletePostAction() {
		$this->ajaxDelete();
	}
	
	// 设置状态
	public function setStatusPostAction() {
		$id = I("id", 0, "intval");
		$status = I("status", 0, "intval");
		$customer = M("Theme");
		if ($customer->where("id={$id}")->setField("status", 1 - $status)) {
			$this->ajaxSuccess();
		} else {
			$this->ajaxError($customer->getError());
		}
	}
	
	
}